<?php

/**
 * uniBoard ACP index page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_index{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Forum age in days
	 *
	 * @var integer
	 */
	
	private $board_days		= 0;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Load logs file
		$appCore -> loadLanguageFile( 'acp_logs');
		$appCore -> loadLanguageFile( 'acp_settings');
		
		// Load templates
		$appCore -> templates['dashboard'] = new tpl_admin_index( $appCore);
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $appCore);
		
		// Cache output
		ob_start();
		
		// Run status check
		$this -> doStatusCheck();
		
		// Display board summary block
		$this -> drawSummary();
		
		// Display quick links (on left) and admin notepad (on right)
		echo $appCore -> templates['dashboard'] -> tpl_index_half_row( $this -> adminNotepad(), $this -> quickActions());
		
		// Display admins on-line
		$this -> adminSessions();
		
		// Display Last 5 ACP Access Logs
		$this -> acpAccessLogs();
		
		// Display Last 5 Admin Logs
		$this -> adminLogs();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_home'], array(), ob_get_clean());
				
	}
	
	/**
	 * Checks this and that in software, and shows error if its no-good
	 *
	 */
	
	function doStatusCheck(){
		
		// Config is writeable?
		if ( is_writable( CONFIG_PATH))
		{
			// Error
			$this -> appCore -> output -> error( $this -> appCore -> lang['acp_status_config'], $this -> appCore -> lang['acp_status_config_title']);
		}
		
		// Software is unstable?
		if ( !PRODUCT_VERSION_STABLE)
		{
			// Error
			$this -> appCore -> output -> error( $this -> appCore -> lang['acp_status_unstable'], $this -> appCore -> lang['acp_status_unstable_title']);
		}
		
		// DevMode?
		if ( DEVELOPER_MODE)
		{
			// Error
			$this -> appCore -> output -> error( $this -> appCore -> lang['acp_status_devmode'], $this -> appCore -> lang['acp_status_devmode_title']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws board summary block
	 * 
	 */
	
	function drawSummary(){
		
		// Let us count forum age in days
		$this -> board_days = ceil( ($this -> appCore -> time - $this -> appCore -> conf['board_start']) / 86400);
		
		// Board summary block
		echo $this -> appCore -> output -> openTableBlock( $this -> appCore -> lang['acp_home_block_overview'], $this -> appCore -> lang['acp_home_block_overview_information']);
		
		// Explain what's what
		echo $this -> appCore -> output -> tableHeader( array(
			array(
				'left_float width_quarter',
				$this -> appCore -> lang['acp_home_overview_item']
			),
			array(
				'left_float width_quarter',
				$this -> appCore -> lang['acp_home_overview_value']
			),
			array(
				'left_float width_quarter',
				$this -> appCore -> lang['acp_home_overview_item']
			),
			array(
				'left_float width_quarter',
				$this -> appCore -> lang['acp_home_overview_value']
			)
		));
		
		// Topics summary
		echo $this -> appCore -> output -> tableRow( '
			<div class="cell left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_topics'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . $this -> appCore -> conf['stat_topics'] . '
			</div>
			<div class="cell left_border left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_dialy'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . $this -> perDay( $this -> appCore -> conf['stat_topics']) . '
			</div>');
			
		// Posts Summary
		echo $this -> appCore -> output -> tableRow( '
			<div class="cell left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_posts'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . $this -> appCore -> conf['stat_posts'] . '
			</div>
			<div class="cell left_border left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_dialy'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . $this -> perDay( $this -> appCore -> conf['stat_posts']) . '
			</div>');
		
		// Members Summary
		echo $this -> appCore -> output -> tableRow( '
			<div class="cell left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_members'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . $this -> appCore -> conf['stat_users'] . '
			</div>
			<div class="cell left_border left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_members_inactive'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . $this -> appCore -> db -> count( 'users', 'user_id > \'0\'') . '
			</div>');
		
		// Uploads and Database size
		echo $this -> appCore -> output -> tableRow( '
			<div class="cell left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_uploads_size'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . class_strings::countSize( filesize( UPLOADS_PATH)) . '
			</div>
			<div class="cell left_border left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_db_size'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . $this -> dbSize() . '
			</div>');
		
		// Board age
		echo $this -> appCore -> output -> tableRow( '
			<div class="cell left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_start_date'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . $this -> appCore -> getDate( $this -> appCore -> conf['board_start']) . '
			</div>
			<div class="cell left_border left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_age'] . ':</b>
			</div>
			<div class="cell left_border left_float acp_home_overview_age">
				' . $this -> appCore -> getAge($this -> appCore -> conf['board_start']) . '
			</div>');
		
		// Board version
		echo $this -> appCore -> output -> tableRow( '
			<div class="cell left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_board_version'] . ':</b>
			</div>
			<div class="cell left_border left_float width_quarter">
				' . PRODUCT_NAME . ' '. PRODUCT_VERSION_LABEL . ' (ID: ' . PRODUCT_VERSION_ID. ')' . '
			</div>
			<div class="cell left_border left_float width_quarter">
				<b>' . $this -> appCore -> lang['acp_home_overview_enviroment'] . ':</b>
			</div>
			<div class="cell left_border left_float acp_home_overview_age">
				' . $_SERVER['SERVER_SOFTWARE'] . '
			</div>');
		
		// Close block
		echo $this -> appCore -> output -> closeTableBlock();
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Returns number of elements per day
	 *
	 * @param integer $value
	 * @return mixed
	 */
	
	function perDay( $value){
		
		// Forum older than day and value bigger than 0?
		if ( $this -> board_days > 0 && $value > 0)
		{
			// Count it
			return round( $value / $this -> board_days, 2);
		}
		else
		{
			// Return value as it is
			return $value;	
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Returns board database size
	 *
	 * @return string
	 */
	
	function dbSize(){
		
		// Size summary
		$db_size = 0;
		
		// Select tables summary
		$this -> appCore -> db -> query( 'SHOW TABLE STATUS');
		
		// Parse result
		while ( $table_result = $this -> appCore -> db -> fetch_assoc())
		{
			// Add data lenght to array
			$db_size += $table_result['Data_length'] + $table_result['Index_length'];
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
		
		// Return counted size
		return class_strings::countSize( $db_size);
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Quick Actions
	 * 
	 * @return string
	 */
	
	function quickActions(){
		
		// Draw it
		$html = $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_home_block_quick_access'], $this -> appCore -> lang['acp_home_block_quick_access_information']);
		
		// Find User
		$html .= $this -> appCore -> output -> openForm( $this -> appCore -> adminLink());
		$html .= $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_home_block_quick_access_user'], $this -> appCore -> drawTextInput( 'user_name', '', '170px') . ' ' . $this -> appCore -> drawButton( '', $this -> appCore -> lang['go'], 'form_button_inline'));
		$html .= $this -> appCore -> output -> closeForm();
		
		// Check IP
		$html .= $this -> appCore -> output -> openForm( $this -> appCore -> adminLink());
		$html .= $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_home_block_quick_access_ip'], $this -> appCore -> drawTextInput( 'check_ip', '', '170px') . ' ' . $this -> appCore -> drawButton( '', $this -> appCore -> lang['go'], 'form_button_inline'));
		$html .= $this -> appCore -> output -> closeForm();
		
		// Run Tool		
		$html .= $this -> appCore -> output -> openForm( $this -> appCore -> adminLink());
		$html .= $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_home_block_quick_access_tool'], $this -> appCore -> drawList( 'tool', array(), '', '176px') . ' ' . $this -> appCore -> drawButton( '', $this -> appCore -> lang['go'], 'form_button_inline'));
		$html .= $this -> appCore -> output -> closeForm();
		
		$html .= $this -> appCore -> output -> closeFormBlock();
		
		// Return ready html
		return $html;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Admins Notepad
	 * 
	 * @return string
	 */
	
	function adminNotepad(){
		
		// Update Notepad?
		if ( $this -> appCore -> _POST( 'update_ntpd') == 1)
		{
			$this -> appCore -> setConfig( 'admins_notepad', $this -> appCore -> _POST( 'admin_notepad'));
		}
		
		// Draw it
		$html = $this -> appCore -> output -> openForm( $this -> appCore -> adminLink());
		$html .= $this -> appCore -> drawHidden( 'update_ntpd', 1);
		$html .= $this -> appCore -> output -> openFormBlock( $this -> appCore -> lang['acp_home_block_notepad'], $this -> appCore -> lang['acp_home_block_notepad_information']);
		
		$html .= $this -> appCore -> output -> tableRow(  $this -> appCore -> templates['dashboard'] -> tpl_notepad( $this -> appCore -> conf['admins_notepad']));
		
		$html .= $this -> appCore -> output -> submitButton( $this -> appCore -> lang['acp_home_block_notepad_save']);
		$html .= $this -> appCore -> output -> closeFormBlock();
		$html .= $this -> appCore -> output -> closeForm();
		
		// Return ready html
		return $html;
		
	}
		
	// ------------------------------------------------------------------------

	/**
	 * Displays block listing admins on-line
	 *
	 */
	
	function adminSessions(){
		
		// Board summary block
		echo $this -> appCore -> output -> openTableBlock( $this -> appCore -> lang['acp_home_block_admin_sessions'], $this -> appCore -> lang['acp_home_block_admin_sessions_information']);
		
		// Columns information
		echo $this -> appCore -> output -> tableHeader( array(
			array(
				'left_float',
				$this -> appCore -> lang['column_user']
			),
			array(
				'right_float width_big',
				$this -> appCore -> lang['column_last_action']
			),
			array(
				'right_float width_big',
				$this -> appCore -> lang['column_session_start']
			),
			array(
				'right_float width_big',
				$this -> appCore -> lang['column_location']
			),
			array(
				'right_float width_small',
				$this -> appCore -> lang['column_ip']
			)
		));
		
		// Select sessions from DB
		$this -> appCore -> db -> query( 'SELECT s.session_start, s.session_last_active, s.session_ip, s.session_location, u.user_id, u.user_name, g.group_prefix, g.group_suffix
		FROM ' . DB_PREFIX . 'sessions_admin s
		LEFT JOIN ' . DB_PREFIX . 'users u ON s.user_id = u.user_id
		LEFT JOIN ' . DB_PREFIX . 'user_groups g ON u.group_id = g.group_id
		ORDER BY s.session_start DESC');
		
		// Draw row with result
		while ( $admin_result = $this -> appCore -> db -> fetch_array())
		{
			
			// Draw row
			echo $this -> appCore -> output -> tableRow( '
				<div class="cell left_float width_long">
					<b>' . $admin_result[6] . htmlspecialchars( $admin_result[5]) . $admin_result[7] . '</b>
				</div>
				<div class="cell left_border right_float width_big center">
					' . $this -> appCore -> getDate( $admin_result[1], 'short') . '
				</div>
				<div class="cell left_border right_float width_big center">
					' . $this -> appCore -> getDate( $admin_result[0], 'short') . '
				</div>
				<div class="cell left_border right_float width_big center">
					' . $this -> appCore -> lang[ 'acp_location_' . $admin_result[3]] . '
				</div>
				<div class="cell left_border right_float width_small center">
					' . $admin_result[2] . '
				</div>');
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
		
		// Close table
		echo $this -> appCore -> output -> closeTableBlock();
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Displays block listing ACP access
	 *
	 */
		
	function acpAccessLogs(){
		
		// Open Block
		echo $this -> appCore -> output -> openTableBlock( '<a href="' . $this -> appCore -> adminLink( 'act=logs_acp_access') . '">' . $this -> appCore -> lang['acp_home_block_admin_access_logs'] . '</a>', $this -> appCore -> lang['acp_home_block_admin_access_logs_information']);
		
		// Header
		echo $this -> appCore -> output -> tableHeader( array(
			array(
				'left_float width_tiny',
				'&nbsp;'
			),
			array(
				'left_float width_long',
				$this -> appCore -> lang['acp_home_block_admin_access_user']
			),
			array(
				'right_float width_big',
				$this -> appCore -> lang['column_date']
			),
			array(
				'right_float width_big',
				$this -> appCore -> lang['column_ip']
			)
		));
		
		// Select Logs from DB
		$this -> appCore -> db -> query( 'SELECT l.log_id, l.user_id, l.user_name, l.log_ip, l.log_date, l.log_acces_state, u.user_name, g.group_prefix, g.group_suffix
		FROM ' . DB_PREFIX . 'logs_admin_access l
		LEFT JOIN ' . DB_PREFIX . 'users u ON l.user_id = u.user_id
		LEFT JOIN ' . DB_PREFIX . 'user_groups g ON u.group_id = g.group_id
		ORDER BY l.log_date DESC
		LIMIT 5');
		
		// Draw row with result
		while ( $admin_result = $this -> appCore -> db -> fetch_array())
		{
			// Draw row
			echo $this -> appCore -> output -> tableRow( '
				<div class="cell left_float width_tiny center">
					' . $this -> appCore -> output -> drawImage( 'state_' . $admin_result[5], $this -> appCore -> lang['log_admin_access_' . $admin_result[5]]) . '
				</div>
				<div class="cell left_border left_float width_long">
					<b>' . htmlspecialchars( $admin_result[1] > 0 ? $admin_result[2] : $admin_result[2]) . '</b>' . ( $admin_result[1] > 0 ? '<div class="item_description">' . $this -> appCore -> lang['log_admin_access_as'] . ' <b>' . $admin_result[7] . htmlspecialchars( $admin_result[6]) . $admin_result[8] . '</b></div>' : '') . '
				</div>
				<div class="cell left_border right_float width_big center">
					' . $this -> appCore -> getDate( $admin_result[4], 'short') . '
				</div>
				<div class="cell left_border right_float width_big center">
					' . $admin_result[3] . '
				</div>');
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
		
		// Close Block
		echo $this -> appCore -> output -> closeTableBlock();
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Displays block listing last 5 Admins Actions
	 *
	 */
	
	function adminLogs(){
		
		// Open Block
		echo $this -> appCore -> output -> openTableBlock( '<a href="' . $this -> appCore -> adminLink( 'act=logs_admin_acts') . '">' . $this -> appCore -> lang['acp_home_block_admin_logs'] . '</a>', $this -> appCore -> lang['acp_home_block_admin_logs_information']);
		
		// Header
		echo $this -> appCore -> output -> tableHeader( array(
			array(
				'left_float',
				$this -> appCore -> lang['column_user']
			),
			array(
				'right_float width_long',
				$this -> appCore -> lang['column_action']
			),
			array(
				'right_float width_big',
				$this -> appCore -> lang['column_date']
			)
		));
		
		// Select Logs from DB
		$this -> appCore -> db -> query( 'SELECT l.log_id, l.user_id, l.user_name, l.log_ip, l.log_date, l.log_content, l.log_target , u.user_id, u.user_name, g.group_prefix, g.group_suffix
		FROM ' . DB_PREFIX . 'logs_admin l
		LEFT JOIN ' . DB_PREFIX . 'users u ON l.user_id = u.user_id
		LEFT JOIN ' . DB_PREFIX . 'user_groups g ON u.group_id = g.group_id
		ORDER BY l.log_date DESC
		LIMIT 5');
		
		// Draw row with result
		while ( $admin_result = $this -> appCore -> db -> fetch_array())
		{
			// Draw row
			echo $this -> appCore -> output -> tableRow( '
				<div class="cell left_float width_long">
					<b>' . $admin_result[9] . htmlspecialchars( $admin_result[1] > 0 ? $admin_result[8] : $admin_result[2]) . $admin_result[10] . '</b><div class="item_description">' . $admin_result[3] . '</div>
				</div>
				<div class="cell left_border right_float width_long">
					<b>' . $this -> appCore -> lang[$admin_result[5]] . '</b>' . ( isset( $admin_result[6][0]) ? '<div class="item_description">' . ( $admin_result[6][0] == '{' ? $this -> appCore -> ifString( $admin_result[6]) : $admin_result[6]) . '</div>' : '') . '
				</div>
				<div class="cell left_border right_float width_big center">
					' . $this -> appCore -> getDate( $admin_result[4], 'short') . '
				</div>');
		}
		
		// Free result
		$this -> appCore -> db -> freeResult();
		
		// Close Block
		echo $this -> appCore -> output -> closeTableBlock();
		
	}
	
}